package com.uetoken.cn.utils;

import android.app.Activity;
import android.hardware.Camera;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Base64;

import com.uetoken.cn.R;
import com.yanzhenjie.album.Action;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumFile;
import com.yanzhenjie.album.api.widget.Widget;
import com.yanzhenjie.durban.Controller;
import com.yanzhenjie.durban.Durban;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * @author 谢杨
 * @time 2018/7/26 14:26
 * @class 描述:拍照或相册选择图片(可裁剪)
 */
public class PhotoUtils {


    public interface PhotoPathListener {
        /**
         * 将拍照或选择相册的路径进行传值
         * @param path
         */
        void getPathSuccess(String path);
    }

    /**
     * 拍照或者选择图片成功的监听
     */
    private PhotoPathListener mPhotoPathListener;

    public void setPhotoPathListener(PhotoPathListener photoPathListener) {
        this.mPhotoPathListener = photoPathListener;
    }

    /**
     * 拍照
     *
     * @param activity
     * @return
     */
    public void takePhoto(final Activity activity) {

        Album.camera(activity).image().onResult(new Action<String>() {
            @Override
            public void onAction(@NonNull String result) {
                mPhotoPathListener.getPathSuccess(result);
            }
        }).onCancel(new Action<String>() {
            @Override
            public void onAction(@NonNull String result) {

            }
        }).start();

    }

    /**
     * 从相册选取一张图片
     *
     * @param activity
     * @return
     */
    public void albumSelect(final Activity activity) {
        Album.image(activity).singleChoice().widget(Widget.newLightBuilder(activity).statusBarColor(ContextCompat.getColor(activity,R.color.color_app_theme))
                .toolBarColor(ContextCompat.getColor(activity,R.color.color_app_theme)).build())
                .camera(false).columnCount(3).onResult(new Action<ArrayList<AlbumFile>>() {
            @Override
            public void onAction(@NonNull ArrayList<AlbumFile> result) {
                mPhotoPathListener.getPathSuccess(result.get(0).getPath());
            }
        }).onCancel(new Action<String>() {
            @Override
            public void onAction(@NonNull String result) {

            }
        }).start();
    }

    /**
     * 裁剪图片
     *
     * @param activity
     * @param photoPath   图片路径
     * @param widthRatio  宽度裁剪比例
     * @param heightRatio 高度裁剪比例
     */
    public void cropImage(final Activity activity, final String photoPath, final float widthRatio, final float heightRatio) {
        //裁剪图片
        Durban.with(activity).title(activity.getString(R.string.str_crop_image)).inputImagePaths(photoPath).outputDirectory
                ("/mnt/sdcard/uetoken/cropimage").maxWidthHeight(300, 300)
                .aspectRatio(widthRatio, heightRatio).gesture(Durban.GESTURE_ALL).controller(
                Controller.newBuilder()
                        .enable(false)
                        .rotation(true)
                        .rotationTitle(true)
                        .scale(true)
                        .scaleTitle(true)
                        .build()).requestCode(200).start();
    }

    /**
     * 将图片地址转换成Base64编码的字符串
     * @param path
     * @return base64编码的字符串
     */
    public static String imageToBase64(String path){
        if(TextUtils.isEmpty(path)){
            return null;
        }
        InputStream is = null;
        byte[] data = null;
        String result = null;
        try{
            is = new FileInputStream(path);
            //创建一个字符流大小的数组。
            data = new byte[is.available()];
            //写入数组
            is.read(data);
            //用默认的编码格式进行编码
            result = Base64.encodeToString(data,Base64.DEFAULT);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(null !=is){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }

    /**
     * 返回true 表示可以使用  返回false表示不可以使用
     */
    public static boolean cameraIsCanUse() {
        boolean isCanUse = true;
        Camera mCamera = null;
        try {
            //针对魅族手机
            mCamera = Camera.open();
            Camera.Parameters mParameters = mCamera.getParameters();
            mCamera.setParameters(mParameters);
        } catch (Exception e) {
            isCanUse = false;
        }

        if (mCamera != null) {
            try {
                mCamera.release();
            } catch (Exception e) {
                e.printStackTrace();
                return isCanUse;
            }
        }
        return isCanUse;
    }
}
